package com.activititest.service.impl;

import com.activititest.appentity.ListidAndTime;
import com.activititest.service.OrdinaryUsersServer;
import org.activiti.engine.IdentityService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdinaryUsersServerImpl implements OrdinaryUsersServer {
    private static final String PROCESS_DEFINE_KEY = "yingji";
    @Resource
    private TaskService taskService;
    @Resource
    private IdentityService identityService;

    @Override
    public List<String> findTasks(String areaname) {
        TaskQuery taskQuery = taskService.createTaskQuery();
        List<Task> tasks = taskQuery.taskCandidateGroup(areaname).taskName("confirm").list();
        List<String> ids = new ArrayList<>();
        for (Task task : tasks) {
            ids.add(task.getId());
            System.out.println("1111111111111111"+task.getId()+"----------------");
        }
        return ids;
    }

    @Override
    public ListidAndTime findSendTasks(String areaname) {
        TaskQuery taskQuery = taskService.createTaskQuery();
        List<Task> runsend = taskQuery.taskCandidateGroup(areaname).taskName("runsend").list();
        ListidAndTime listidAndTime = new ListidAndTime();
        List<String> ids = new ArrayList<>();
        List<Date> times = new ArrayList<>();
        for (Task task : runsend) {
            ids.add(task.getId());
            times.add(task.getCreateTime());
        }
        listidAndTime.setIds(ids);
        listidAndTime.setTimes(times);
        return listidAndTime;
    }

    @Override
    public List<Task> findQueryTasks(String areaname) {
        TaskQuery taskQuery = taskService.createTaskQuery();
        List<Task> runsend = taskQuery.taskCandidateGroup(areaname).taskName("runsend").list();
        return runsend;
    }

    //确认
    @Override
    public String QueryTasks(String areaid, String taskid) {
        Task task = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        taskService.complete(taskid);
        Task currentTask = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
        taskService.addCandidateGroup(currentTask.getId(), areaid);
        return currentTask.getId();
    }

    @Override
    public List<Task> findCommitTasks(String areaname) {
        return null;
    }

    //提交污染物工作结果
    @Override
    public String CommitTaskss(String taskid) {
        System.out.println(taskid);
        Task task = taskService.createTaskQuery().taskId(taskid).singleResult();
        System.out.println("zzzzzzzzzzzzzzzzzzz"+task.getProcessInstanceId()+"zzzzzzzzzzzzzzzzzzzzzzzz");
        String processInstanceId = task.getProcessInstanceId();
        taskService.complete(taskid);
//        Task currentTask = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();

        return processInstanceId;
    }
}
